package com.hippo4j.plugin.manager;

import com.hippo4j.plugin.ExecuteAwarePlugin;
import com.hippo4j.plugin.RejectAwarePlugin;
import com.hippo4j.plugin.ShutdownAwarePlugin;
import com.hippo4j.plugin.ThreadPoolPlugin;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class DefaultThreadPoolPluginManager implements ThreadPoolPluginManager {

    private Map<String, ThreadPoolPlugin> pluginMap = new ConcurrentHashMap<>();

    private List<ExecuteAwarePlugin> executeAwarePluginList = new CopyOnWriteArrayList<>();

    private List<ShutdownAwarePlugin> shutdownAwarePluginList = new CopyOnWriteArrayList<>();

    private List<RejectAwarePlugin> rejectAwarePluginList = new CopyOnWriteArrayList<>();

    public void registerPlugin(ThreadPoolPlugin plugin) {
        if (plugin == null) {
            throw new RuntimeException("插件不能为空");
        }

        String pluginId = plugin.getPluginId();
        pluginMap.put(pluginId, plugin);

        if (plugin instanceof ExecuteAwarePlugin) {
            executeAwarePluginList.add((ExecuteAwarePlugin) plugin);
        }
        if (plugin instanceof ShutdownAwarePlugin) {
            shutdownAwarePluginList.add((ShutdownAwarePlugin) plugin);
        }
        if (plugin instanceof RejectAwarePlugin) {
            rejectAwarePluginList.add((RejectAwarePlugin) plugin);
        }
    }

    public List<ExecuteAwarePlugin> getExecuteAwarePluginList() {
        return executeAwarePluginList;
    }

    public List<ShutdownAwarePlugin> getShutdownAwarePluginList() {
        return shutdownAwarePluginList;
    }

    public List<RejectAwarePlugin> getRejectAwarePluginList() {
        return rejectAwarePluginList;
    }
}
